package com.dq.demo.ui.activity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.dq.demo.manager.ActManager;
import com.dq.demo.model.http.OkgoUtil;
import com.dq.demo.ui.fragment.FoundFragment;
import com.dq.demo.ui.fragment.HomeFragment;
import com.dq.demo.ui.fragment.MeFragment;
import com.dq.demo.ui.fragment.ProjectFragment;
import com.dq.demo.ui.fragment.RecommendedFragment;
import com.dq.demo.util.StatusBarUtil;
import com.dq.wallpaper.R;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.main_tab_1)
    public RadioButton mainTab1;
    @BindView(R.id.main_table)
    public RadioGroup mainTable;

    private HomeFragment homeFragment;
    private FoundFragment foundFragment;
    private ProjectFragment projectFragment;
    private MeFragment meFragment;
    private RecommendedFragment recommendedFragment;

    private FragmentManager manager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //强制竖屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
        setStatusBar();
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mainTable.setOnCheckedChangeListener(this);
        mainTab1.setChecked(true);
    }

    public void setStatusBar() {
        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtil.setRootViewFitsSystemWindows(this, true);
        //设置状态栏透明
        StatusBarUtil.setTranslucentStatus(this);
    }

    /**
     * 设置状态栏字体颜色
     *
     * @param colors 颜色值
     */
    public void setStatusBarColor(int colors) {
        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
            StatusBarUtil.setStatusBarColor(this, colors);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        selectTab(checkedId);
    }

    //进行选中Tab的处理
    private void selectTab(int id) {
        //获取FragmentManager对象
        if (manager == null) {
            manager = getSupportFragmentManager();
        }
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (id) {
            case R.id.main_tab_1:
                if (homeFragment == null) {
                    homeFragment = HomeFragment.createFragment();
                    transaction.add(R.id.main_fragment, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                break;
            case R.id.main_tab_2:
                if (recommendedFragment == null) {
                    recommendedFragment = RecommendedFragment.createFragment();
                    transaction.add(R.id.main_fragment, recommendedFragment);
                } else {
                    transaction.show(recommendedFragment);
                }
                break;
            case R.id.main_tab_3:
                if (foundFragment == null) {
                    foundFragment = FoundFragment.createFragment();
                    transaction.add(R.id.main_fragment, foundFragment);
                } else {
                    transaction.show(foundFragment);
                }
                break;
            case R.id.main_tab_4:
                if (projectFragment == null) {
                    projectFragment = ProjectFragment.createFragment();
                    transaction.add(R.id.main_fragment, projectFragment);
                } else {
                    transaction.show(projectFragment);
                }
                break;
            case R.id.main_tab_5:
                if (meFragment == null) {
                    meFragment = MeFragment.createFramgemt();
                    transaction.add(R.id.main_fragment, meFragment);
                } else {
                    transaction.show(meFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (recommendedFragment != null) {
            transaction.hide(recommendedFragment);
        }
        if (foundFragment != null) {
            transaction.hide(foundFragment);
        }
        if (projectFragment != null) {
            transaction.hide(projectFragment);
        }
        if (meFragment != null) {
            transaction.hide(meFragment);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            new QMUIDialog.MessageDialogBuilder(this)
                    .setTitle("温馨提示")
                    .setMessage("确认退出吗?")
                    .setCancelable(false)//禁止按下返回false
                    .setCanceledOnTouchOutside(false)//禁止触摸false
                    .addAction("取消", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            dialog.dismiss();
                        }
                    })
                    .addAction("确定", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            dialog.dismiss();
                            MainActivity.this.finish();
                        }
                    }).show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkgoUtil.getInstance().cancelAll();
        ActManager.getAppManager().AppExit(this);
    }
}